package com.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import com.tank.TankClient;
public class NetClient {
	
	private static  int Udp_Port_Start=2222;
	
	private int udpPort;
	
	 TankClient  tc;
	
	public NetClient(TankClient tc){
		udpPort = Udp_Port_Start++;
		this.tc = tc;
	}
	
	public void connet(String ip,int port){
		Socket socket = null;
		try {
			 socket = new Socket(ip,port);
			DataOutputStream dos  = new DataOutputStream(socket.getOutputStream());
			dos.writeInt(udpPort);
			
			
			//接收服务器为此客户端生成的ID号
			DataInputStream dis  = new DataInputStream(socket.getInputStream());
			int netId = dis.readInt();
			tc.myTank.netId= netId;
			
			System.out.print("conneted to server! The server give me a id:"+ netId);
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(socket!=null){
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	public static void main(String[] args) {

	}

}
